package com.jnu.HomeworkApplication.View;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.jnu.HomeworkApplication.data.Note;

import java.util.ArrayList;
import java.util.Random;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder surfaceHolder;

    public GameView(Context context) {
        super(context);
        initView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        surfaceHolder=getHolder();
        surfaceHolder.addCallback(this);
    }

    private DrawThread drawThread;
    private boolean isTouched = true;
    private float touchedX = -1;
    private float touchedY = -1;


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        drawThread = new DrawThread();
        drawThread.start();
        GameView.this.setOnTouchListener((v, event) -> {
            if(MotionEvent.ACTION_DOWN==event.getAction()){
                touchedX = event.getRawX();
                touchedY = event.getRawY();
                isTouched = true;
                return true;
            }
            return false;
        });
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        drawThread.setStop();
        try {
            drawThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    private class DrawThread extends Thread{

        private boolean isRunning = true;
        private ArrayList<Note> NoteList = new ArrayList<>();

        public void addNote(int direction){
            switch(direction){
                case 0:
                    NoteList.add(new Note(150,100,GameView.this.getHeight()));
                    break;
                case 1:
                    NoteList.add(new Note(350,100,GameView.this.getHeight()));
                    break;
                case 2:
                    NoteList.add(new Note(550,100,GameView.this.getHeight()));
                    break;
                default:
                    NoteList.add(new Note(750,100,GameView.this.getHeight()));
                    break;
            }
        }

        public DrawThread(){

        }

        public void setStop(){
            isRunning = false;
        }

        @Override
        public void run() {
            super.run();
            Canvas canvas = null;
            int countRail = 0;
            int hitCount = 0;

            while(isRunning) {
                try {
                    canvas = surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.WHITE);

                    if(GameView.this.isTouched){
                        for(int index=0;index<NoteList.size();index++){
                            if(NoteList.get(index).isHit(touchedX,touchedY)){
                                hitCount++;
                            }
                        }
                    }
                    Random random = new Random();


                    if(countRail==0){
                        addNote(random.nextInt(2));
                    }
                    else if(countRail==5){
                        addNote(random.nextInt(2)+2);
                    }
                    else if(countRail==10){
                        addNote(random.nextInt(2));
                    }
                    else if(countRail==15){
                        addNote(random.nextInt(2)+2);
                    }
                    else if(countRail==20){
                        countRail=-1;
                    }
                    countRail++;

                    for(int index=0;index<NoteList.size();index++)
                    {
                        if(NoteList.get(index).MoveNote()){
                            NoteList.get(index).setY(NoteList.get(index).getY()+20);
                        }
                        else {
                            NoteList.remove(index);
                        }
                    }
                    for(int index=0;index<NoteList.size();index++)
                    {
                        NoteList.get(index).drawNote(canvas);
                    }
                    GameView.this.isTouched=false;
                    Paint paint=new Paint();
                    paint.setTextSize(100);
                    paint.setColor(Color.GREEN);
                    canvas.drawText("Combo "+hitCount,100,100,paint);

                } catch (Exception ignored) {

                } finally {
                    if (canvas != null) {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }

                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
